﻿namespace GoodAdmin.Web.Core;

/// <summary>
/// 组织管理控制器
/// </summary>
[ApiDescriptionSettings("System", Tag = "组织管理")]
[Route("system/org")]
[RoleDataScope]
public class OrgController: IDynamicApiController
{
    private readonly IExtraOrgService _extraOrgService;
    private readonly ISysOrgService _sysOrgService;

    public OrgController(IExtraOrgService extraOrgService,ISysOrgService sysOrgService)
	{
        _extraOrgService = extraOrgService;
        _sysOrgService = sysOrgService;
    }
    #region GET
    /// <summary>
    /// 分页查询
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet("page")]
    [DisplayName("分页查询")]
    [RoleDataScope]
    public async Task<SqlSugarPagedList<SysOrg>> GetOrgPageList([FromQuery] OrgPageInput input)
    {
        return await _extraOrgService.GetOrgPageList(input);
    }

    [HttpGet("orgSelector")]
    [DisplayName("组织选择器")]
    [AllowLoginUser]
    public async Task<List<OrgSimpleInfo>> OrgSelector()
    {
        return await _sysOrgService.OrgSelector();
    }

    /// <summary>
    /// 获取组织列表树
    /// </summary>
    /// <returns></returns>
    [HttpGet("orgTree")]
    [DisplayName("获取组织列表树")]
    [RoleDataScope]
    public async Task<List<OrgSimpleInfo>> GetOrgTree()
    {
        return await _extraOrgService.GetOrgTree();
    }
    /// <summary>
    /// 模板下载
    /// </summary>
    /// <returns></returns>
    [HttpGet("templateDownload")]
    [DisplayName("模板下载")]
    public FileStreamResult TemplateDownload()
    {
        return _extraOrgService.TemplateDownload();
    }

    /// <summary>
    /// 错误报告下载
    /// </summary>
    /// <returns></returns>
    [HttpGet("errorDownload")]
    [DisplayName("错误报告下载")]
    public async Task<FileStreamResult> ErrorDownload([FromQuery] BaseIdInput input)
    {
        return await _extraOrgService.ErrorDownload(input);
    }
    #endregion

    #region POST
    /// <summary>
    /// 添加组织
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("add")]
    [DisplayName("添加组织")]
    [RoleDataScope]
    public async Task AddOrg(OrgAddInput input)
    {
        await _extraOrgService.AddOrg(input);
    }

    /// <summary>
    /// 批量添加组织
    /// </summary>
    /// <param name="file"></param>
    /// <returns></returns>
    [HttpPost("batchImport")]
    [DisplayName("批量添加组织")]
    [RoleDataScope]
    public async Task<ExcelImportResult> BatchImport(IFormFile file)
    {
        return await _extraOrgService.ImportOrg(file);
    }

    /// <summary>
    /// 删除组织
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("delete")]
    [DisplayName("删除组织")]
    [RoleDataScope]
    public async Task DeleteOrg(BaseIdInput input)
    {
        await _extraOrgService.DeleteOrg(input);
    }
    
    /// <summary>
    /// 更新组织
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost("update")]
    [DisplayName("更新组织")]
    [RoleDataScope]
    public async Task UpdateOrg(OrgUpdateInput input)
    {
        await _extraOrgService.UpdateOrg(input);
    }
    #endregion
}